<?php
include_once "baseChannel.php";

class flowRecharge extends baseChannel
{

    public function __construct($setting)
    {
        parent::__construct($setting);
        $this->appId = $setting['appId'];
        // $this->apikey = $setting['apikey'];
    }
    public function submitOrder($mobile, $code, $params = [])
    {
        $city = explode('-', $params['area_name'])[1];
        $map = [
            "深圳" => "sz",
            "东莞" => "dg",
            "广州" => "gz",
            "湛江" => "zj",
        ];
        $codeMap = [
            "东莞" => "5GB7DAYS_DG",
            "广州" => "JYPT200.220228122717.0",
            "深圳" => "shsh.75500200003646",
            "湛江" => "ZJ7T5GBWL",
        ];
        $data = [
            "shopOpenId" => $this->appId,
            "productId" => $codeMap[$city],
            // $code
            "mobile" => $mobile,
            "city" => $map[$city]
        ];
        $return_str = $this->httpRequest($this->submit_order_url, 'POST', $data);
        $return_data = json_decode($return_str, 1);
        $ret = [
            'error' => -1,
            'msg' => '初始化',
            'channel_order' => $return_data['bizOrderNo'] ?? '',
            'post_data' => json_encode($data),
            'channel_data' => $return_str,
            'code' => 200,
            'channel_order_price' =>  0,
            'channel_balance' =>  null,
        ];
        if ($return_data['code'] == 10000 && $return_data['data']['orderStatus'] == 1) {
            // 成功
            $ret['error'] = 0;
            $ret['order_status'] = 3;
            $ret['msg'] = $return_data['data']['description'] ?? '下单成功';
        } else {
            // 失败
            // if (isset($return_data['code'])) {
            $ret['error'] = 1;
            $ret['order_status'] = 4;
            $ret['code'] = 500;
            $ret['msg'] = $return_data['data']['description'] ?? '下单失败';
            // }
        }
        return $ret;
        // return;
    }
    public function queryOrder($params)
    {
        $url = $this->query_order_url;
        $post_data = [
            'shopOpenId' => $this->appId,
            'orderNo' => $params['channel_order_sn'],
        ];
        // $sign = $this->getSign($post_data);
        // $post_data['sign'] = $sign;

        $return_str = $this->httpRequest($url, 'POST', $post_data);
        $return_data = json_decode($return_str, 1);

        $ret = [
            'error' => 0,
            'order_status' => 2,
            'msg' => '初始化',
            'pz' =>  '', //凭证
            'post_data' => json_encode($post_data), //提交字符串
            'channel_data' => $return_str, //返回字符串
            'code' => 200
        ];
        //充值中
        // if ($return_data['nRtn'] == 5011 || $return_data['nRtn'] == 5019) {
        //     $ret['msg'] = '充值中:' . $return_data['szRtnCode'];
        // }
        // 成功
        if ($return_data['code'] == 10000  && $return_data['data']['orderStatus'] == 1) {
            $ret['order_status'] = self::OrderSuccess;
            $ret['msg'] = '充值完成:' . $return_data['description'];
        }
        // 失败
        if ($return_data['code'] == 10000 && $return_data['data']['orderStatus'] == 2) {
            $ret['order_status'] = self::OrderFail;
            $ret['msg'] = '充值失败:' . $return_data['description'];
        }
        return $ret;
    }
}
